package org.song.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.annotation.LogRecord;
import org.song.annotation.PowerEquals;
import org.song.annotation.RequestTimeRecord;
import org.song.exception.result.AjaxResult;
import org.song.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;

@Api(tags = "文件上传下载控制器")
@RequestMapping(value = "/file")
@RestController
public class FileController {

    @Autowired( required = true )
    @Qualifier( value = "fileServiceImpl" )
    private FileService fileService;

    /**
     * 上传博客主图
     * @param file
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @RequestMapping(value = "/uploadBlogMainImage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上传博客主图", notes = "上传博客主图")
    public AjaxResult<String> uploadBlogMainImage(  @ApiParam( value = "使用springMVC文件实例接受" ) @RequestParam( value = "file" , required = true ) MultipartFile file ) {
        return AjaxResult.success( fileService.uploadBlogMainImage( file ) );
    }

    /**
     * 根据对象名从OSS获取博客主图
     * @param objectName
     * @return
     */
    @RequestTimeRecord
    @RequestMapping(value = "/getBlogMainImageFromOssByObjectName", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据对象名从OSS获取博客主图", notes = "根据对象名从OSS获取博客主图")
    public AjaxResult<URL> getBlogMainImageFromOssByObjectName( @RequestParam( value = "objectName" , required = true ) @ApiParam( value = "OSS保存实例名" ) String objectName ) {
        return AjaxResult.success(fileService.getBlogMainImageFromOssByObjectName( objectName , null ));
    }

    /**
     * 博客内容图片上传
     * @param file
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation( value = "博客内容图片上传" , notes = "博客内容图片上传" )
    @ResponseBody
    @RequestMapping( value = "/uploadBlogContentImage" , method = RequestMethod.POST )
    public AjaxResult<String> uploadBlogContentImage(  @ApiParam( value = "springMVC文件实例接受" ) @RequestParam( value = "file" , required = true ) MultipartFile file ) {
        return AjaxResult.success( fileService.uploadBlogContentImage( file ) );
    }

    /**
     * 获取博客内容图片
     * @param objectName
     * @return
     */
    @RequestTimeRecord
    @ApiOperation( value = "获取博客内容图片")
    @ResponseBody
    @RequestMapping( value = "/getBlogContentImage" , method = RequestMethod.GET)
    public AjaxResult<URL> getBlogContentImage( @ApiParam( value = "博客主图保存实例名") @RequestParam( value = "objectName" , required = true ) String objectName ) {
        return AjaxResult.success( fileService.getBlogContentImage( objectName , System.currentTimeMillis() * 3600 * 1000 ) );
    }

}
